﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using AcademySystem.Service.DTO;
using AcademySystem.DAL;
using System.ServiceModel.Activation;
using System.Web.Security;
using AcademySystem.DAL.Helpers;

namespace AcademySystem.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class AcademySystemService : IAcademySystemService
    {
        public IEnumerable<CourseDTO> GetAllCourses()
        {
            IEnumerable<Course> cources = AcademySystemDAO.GetAllCources();
            List<CourseDTO> courceDtos = new List<CourseDTO>();
            foreach (Course course in cources)
            {
                CourseDTO courseDto = CourseDTO.CreateCourseDTO(course);
                courceDtos.Add(courseDto);
            }

            return courceDtos;
        }

        public IEnumerable<CourseDTO> GetCourses(int skip, int count)
        {
            IEnumerable<Course> courses = AcademySystemDAO.GetCources(skip, count);
            return ConvertCources(courses);
        }

        public IEnumerable<CourseDTO> GetTopCourses(int count)
        {
            IEnumerable<Course> courses = AcademySystemDAO.GetTopCourses(count);
            return ConvertCources(courses);
        }

        public IEnumerable<CourseDTO> GetUserCourses(string userId)
        {
            Guid guidUserId;
            if (Guid.TryParse(userId, out guidUserId))
            {
                IEnumerable<Course> courses = AcademySystemDAO.GetUserCourses(guidUserId);
                return ConvertCources(courses);
            }
            return new List<CourseDTO>();
        }

        public IEnumerable<SubscriptionDTO> GetUserSubscriptions(string userId)
        {
            Guid guidUserId;
            if (Guid.TryParse(userId, out guidUserId))
            {
                IEnumerable<Subscription> subscriptions = AcademySystemDAO.GetUserSubscriptions(guidUserId);
                return ConvertSubscriptions(subscriptions);
            }
            return new List<SubscriptionDTO>();
        }

        public void ToggleSubscription(string userId, int courseId)
        {
            Guid guidUserId = Guid.Parse(userId);
            AcademySystemDAO.ToggleSubscription(guidUserId, courseId);
        }

        public AuthenticationResult Authenticate(string username, string password)
        {
            var result = AcademySystemDAO.GetAuthenticationResult(username, password);
            return result;
        }

        private IEnumerable<CourseDTO> ConvertCources(IEnumerable<Course> courses)
        {
            List<CourseDTO> coursesDto = new List<CourseDTO>();
            foreach (Course course in courses)
            {
                coursesDto.Add(CourseDTO.CreateCourseDTO(course));
            }
            return coursesDto;
        }

        private IEnumerable<SubscriptionDTO> ConvertSubscriptions(IEnumerable<Subscription> subscriptions)
        {
            List<SubscriptionDTO> subscriptionsDto = new List<SubscriptionDTO>();
            foreach (Subscription subscription in subscriptions)
            {
                subscriptionsDto.Add(SubscriptionDTO.CreateSubscriptionDTO(subscription));
            }
            return subscriptionsDto;
        }
    }
}
